package com.jkt.rms.commons.utils;


public class SecurityHelper {

    private static char realToConv(char in){
        char out = ' ';
        int i = realchars_.indexOf(in);
        if(i >= 0)
            out = convchars_.charAt(i);
        else
            out = in;
        return out;
    }

    private static char convToReal(char in){
        char out = ' ';
        int i = convchars_.indexOf(in);
        if(i >= 0)
            out = realchars_.charAt(i);
        else
            out = in;
        return out;
    }

    public static String encrypt(String input){
        StringBuffer endResult = new StringBuffer("");
        int chars = input != null ? input.length() : 0;
        if(chars > 0){
            int poscount = 0;
            for(int i = 0; i < chars; i++)
                poscount += realchars_.indexOf(input.charAt(i)) + 1;

            int t = poscount % 62 - 1;
            if(t < 0)
                t = 0;
            input = input + realchars_.charAt(t);
            chars++;
            poscount = 0;
            for(int i = 0; i < chars; i++)
                poscount += realchars_.indexOf(input.charAt(i)) + 1;

            t = poscount % 62 - 1;
            if(t < 0)
                t = 0;
            input = realchars_.charAt(t) + input;
            chars++;
            for(int i = 0; i < chars; i++)
                endResult.append(realToConv(input.charAt(i)));

            StringBuffer temp = endResult;
            endResult = new StringBuffer("");
            int hyphens = 0;
            for(int i = 0; i < chars + hyphens; i++){
				if((i + 1) % 5 == 0){
					endResult.append("-");
					hyphens++;
				}
				else{
					endResult.append(temp.charAt(i - hyphens));
				}
			}
        }
        return endResult.toString();
    }

    public static String decrypt(String input){
        StringBuffer endResult = new StringBuffer("");
        String output = "";
        int chars = (input != null ? input.length() : 0);
        if(chars > 0){
            StringBuffer temp = new StringBuffer("");
            for(int i = 0; i < chars; i++){
                if(input.charAt(i) != '-'){
                    temp.append(input.charAt(i));
				}
			}

            StringBuffer temp2 = new StringBuffer("");
            char initialcheck = ' ';
            for(int i = 0; i < temp.length(); i++)
                if(i == 0)
                    initialcheck = convToReal(temp.charAt(i));
                else
                    temp2.append(convToReal(temp.charAt(i)));

//			System.out.println(initialcheck + temp2 );

            int poscount = 0;
            for(int i = 0; i < temp2.length(); i++)
                poscount += realchars_.indexOf(temp2.charAt(i)) + 1;

            int t = poscount % 62 - 1;
            if(t < 0)
                t = 0;
            if(initialcheck == realchars_.charAt(t)){
                char finalcheck = temp2.charAt(temp2.length() - 1);
                temp2.setLength(temp2.length() - 1);
                poscount = 0;
                for(int i = 0; i < temp2.length(); i++)
                    poscount += realchars_.indexOf(temp2.charAt(i)) + 1;

                t = poscount % 62 - 1;
                if(t < 0)
                    t = 0;
                if(finalcheck == realchars_.charAt(t))
                    output = temp2.toString();
                else
                    System.err.println("ERROR: " + "Corrupt input - failed to decrypt");
            }
			else{
                System.err.println("ERROR::" + "Corrupt input - failed to decrypt");
            }
        }
        return output;
    }

    private static String realchars_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz!\243$%^&*()_-+=[]:;@#|\\/<>,.?'~\"";
    private static String convchars_= "09PLKAMB1N28RGJ6C5ZOYD3S4WEUHIQFVT7Xkwictabpqgzoedvsmfluryhnjx&$!)+@>:([=#/<\\.],|?~\"'\243%^*_;-";
    public static final int SUCCESS = 1;
    public static final int FAILURE = 2;
    
    public static void main(String args[]) {
    	System.out.println(encrypt("admin"));
    }
   
    
}
